package com.ugoodtech.mdcc.core.service.impl;

import com.mysema.query.BooleanBuilder;
import com.ugoodtech.mdcc.core.domain.*;
import com.ugoodtech.mdcc.core.dto.ComplaintSearch;
import com.ugoodtech.mdcc.core.helper.ComplaintNum;
import com.ugoodtech.mdcc.core.helper.ProvinceBrandPoJo;
import com.ugoodtech.mdcc.core.helper.ProvinceBrandSearchHelper;
import com.ugoodtech.mdcc.core.helper.SearchVoId;
import com.ugoodtech.mdcc.core.repository.*;
import com.ugoodtech.mdcc.core.service.ComplaintSearchService;
import com.ugoodtech.mdcc.core.service.DepartmentCoreService;
import com.ugoodtech.mdcc.core.service.SearchPermissionsService;

import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ComplaintSearchServiceImpl implements ComplaintSearchService {

    /**
     * 投诉报告Repository
     */
    @Autowired
    private ComplaintReportRepository complaintReportRepository;

    /**
     * 投诉报告答案Repository
     */
    @Autowired
    private ComplaintReportDataRepository complaintReportDataRepository;

    @Autowired
    private UserProfileRepository userProfileRepository;

    @Autowired
    private QuestionAnswerOptionRepository questionAnswerOptionRepository;

    @Autowired
    private ComplaintUserRepository complaintUserRepository;

    @Autowired
    private DepartmentRepository departmentRepository;

    @Autowired
    private DepartmentCoreService departmentCoreService;

    @Autowired
    private ProvinceBrandSearchHelper provinceBrandSearchHelper;

    @Autowired
    private SearchPermissionsService searchPermissionsService;

    @Autowired
    private VStarCodeNumRepository vStarCodeNumRepository;

    /**
     * 查询投诉报告
     *
     * @param complaintSearch
     * @param clientUser
     * @return
     */
    @Override
    public List<Long> searchComplaintReport(ComplaintSearch complaintSearch, User clientUser) {
        UserProfile userProfile = userProfileRepository.findByUserId(clientUser.getId());

        BooleanBuilder builder = new BooleanBuilder();
        QComplaintReport qComplaintReport = QComplaintReport.complaintReport;
        builder.and(qComplaintReport.deleted.eq(false));
        builder.and(qComplaintReport.enabled.eq(true));

        BooleanBuilder userBuilder = new BooleanBuilder();
        QComplaintReportData qComplaintReportData = QComplaintReportData.complaintReportData;
        userBuilder.and(qComplaintReportData.deleted.eq(false));
        userBuilder.and(qComplaintReportData.enabled.eq(true));
            //使用用户自己所属省份查询对应投诉答案表里存在的结果 or (questionid=189 and answerother in())
            BooleanBuilder userOrBuilder = new BooleanBuilder();
                BooleanBuilder userProfileBuilder = new BooleanBuilder();
                userProfileBuilder.and(qComplaintReportData.deleted.eq(false));
                userProfileBuilder.and(qComplaintReportData.enabled.eq(true));
                userProfileBuilder.and(qComplaintReportData.questionId.eq(189L));
                List<String> ownNationStr = new ArrayList<>();
                for (Nation nation : userProfile.getNationList()) {
                    ownNationStr.add(nation.getProvince());
                }
                userProfileBuilder.and(qComplaintReportData.answerOther.in(ownNationStr));
            userOrBuilder.or(userProfileBuilder);
                //先查出用户自己所属品牌
                List<String> ownBrandStr = new ArrayList<>();
                for (Brand brand : userProfile.getBrandList()) {
                    ownBrandStr.add(brand.getBrandName());
                }
                //从答案表中找出针对这题的所有品牌答案，再遍历获取名字相同品牌对应的答案id
                QQuestionAnswerOption qQuestionAnswerOption = QQuestionAnswerOption.questionAnswerOption;
                BooleanBuilder questionBuilder = new BooleanBuilder();
                questionBuilder.and(qQuestionAnswerOption.deleted.eq(false));
                questionBuilder.and(qQuestionAnswerOption.enabled.eq(true));
                questionBuilder.and(qQuestionAnswerOption.question.id.eq(194L));
                List<QuestionAnswerOption> questionAnswerOptions = (List<QuestionAnswerOption>) questionAnswerOptionRepository.findAll(questionBuilder);
                List<String> ownBrandIDs = new ArrayList<>();
                for (QuestionAnswerOption question : questionAnswerOptions){
                    for (String str : ownBrandStr) {
                        if (str.equals(question.getName())) {
                            ownBrandIDs.add(question.getId()+"");
                        }
                    }
                }
                userProfileBuilder = new BooleanBuilder();
                userProfileBuilder.and(qComplaintReportData.deleted.eq(false));
                userProfileBuilder.and(qComplaintReportData.enabled.eq(true));
                userProfileBuilder.and(qComplaintReportData.questionId.eq(194L));
                BooleanBuilder questionOrBuilder = new BooleanBuilder();
                for (String id : ownBrandIDs){
                    questionOrBuilder.or(qComplaintReportData.answerId.like("%"+id+"%"));
                }
                userProfileBuilder.and(questionOrBuilder);
            userOrBuilder.or(userProfileBuilder);
        userBuilder.and(userOrBuilder);
        List<ComplaintReportData> dataList = (List<ComplaintReportData>) complaintReportDataRepository.findAll(userBuilder);
        if (dataList == null || dataList.size() == 0){
            return new ArrayList<>();
        }
        List<Long> ownIds = new ArrayList<>();
        for (ComplaintReportData reportData: dataList){
            ownIds.add(reportData.getComplaintReport().getId());
        }
        builder.and(qComplaintReport.id.in(ownIds));

        if (StringUtils.isNotEmpty(complaintSearch.getComplaintLevel())) {
            Integer[] level = (Integer[]) ConvertUtils.convert(complaintSearch.getComplaintLevel().split(","),Integer.class);
            builder.and(qComplaintReport.complaintLevel.in(level));
        }
        if (StringUtils.isNotEmpty(complaintSearch.getComplaintStatus())) {
            Integer[] statuses = (Integer[]) ConvertUtils.convert(complaintSearch.getComplaintStatus().split(","),Integer.class);
            BooleanBuilder builderOr = new BooleanBuilder();
            for (Integer status :statuses){
                if (complaintSearch.getComplaintStatus().equals(6)) {
                    builder.and(qComplaintReport.followUser.isNull());
                }
                if (complaintSearch.getComplaintStatus().equals(5)) {
                    builder.and(qComplaintReport.timeOut.eq(true));
                }
                builderOr.or(qComplaintReport.reportStatus.in(status));
            }
            builder.and(builderOr);
        }
        if (null != complaintSearch.getStartTime() && complaintSearch.getStartTime() != 0){
            builder.and(qComplaintReport.creationTime.goe(new Date(complaintSearch.getStartTime())));
        }if(null != complaintSearch.getEndTime() && complaintSearch.getEndTime() != 0) {
            builder.and(qComplaintReport.creationTime.lt(new Date(complaintSearch.getEndTime())));
        }
        if (StringUtils.isNotEmpty(complaintSearch.getUserName())){
            QUserProfile qUserProfile = QUserProfile.userProfile;
            BooleanBuilder booleanBuilder = new BooleanBuilder();
            booleanBuilder.and(qUserProfile.deleted.eq(false));
            booleanBuilder.and(qUserProfile.enabled.eq(true));
            booleanBuilder.and(qUserProfile.realName.eq(complaintSearch.getUserName()));
            List<UserProfile> users = (List<UserProfile>) userProfileRepository.findAll(booleanBuilder);
            if (users == null || users.size()==0){
                return new ArrayList<>();
            }
            List<Long> userIds = new ArrayList<>();
            for (UserProfile user : users){
                userIds.add(user.getId());
            }
            builder.and(qComplaintReport.user.id.in(userIds));
        }

        if (StringUtils.isNotEmpty(complaintSearch.getUserName()) || StringUtils.isNotEmpty(complaintSearch.getDepartmentIds())) {
            QUserProfile qUserProfile = QUserProfile.userProfile;
            BooleanBuilder booleanBuilder = new BooleanBuilder();
            booleanBuilder.and(qUserProfile.deleted.eq(false));
            booleanBuilder.and(qUserProfile.enabled.eq(true));
            boolean flag = false;
            if (StringUtils.isNotEmpty(complaintSearch.getUserName())) {
                flag = true;
                booleanBuilder.and(qUserProfile.realName.like("%" + complaintSearch.getUserName() + "%"));
            }
            if (StringUtils.isNotEmpty(complaintSearch.getDepartmentIds())) {
                flag = true;
                Long[] departments = (Long[]) ConvertUtils.convert(complaintSearch.getDepartmentIds().split(","),Long.class);
                booleanBuilder.and(qUserProfile.user.userFlag.in(0,2));
                booleanBuilder.and(qUserProfile.user.userType.in(UserType.admin,UserType.dealers,UserType.insiders));
                for (Long id : departments){
                    booleanBuilder.or(qUserProfile.departmentList.any().id.eq(id));
                }
            }
            if (flag) {
                List<UserProfile> userProfileList = (List<UserProfile>) userProfileRepository.findAll(booleanBuilder);
                if (userProfileList == null || userProfileList.size() == 0) {
                    return new ArrayList<>();
                }
                List<Long> userIds = new ArrayList<>();
                for (UserProfile user : userProfileList) {
                    userIds.add(user.getUser().getId());
                }
                builder.and(qComplaintReport.user.id.in(userIds));
            }
        }
        if (null != complaintSearch.getValidTime()) {
            builder.and(qComplaintReport.timeOut.eq(complaintSearch.getValidTime()));
        }
        if ((null != complaintSearch.getComplaintNum() && complaintSearch.getComplaintNum() > 0) || StringUtils.isNotEmpty(complaintSearch.getStationCode())) {
            boolean flag = false;
            BooleanBuilder builder1 = new BooleanBuilder();
            builder1.and(qComplaintReportData.enabled.eq(true));
            builder1.and(qComplaintReportData.deleted.eq(false));
            builder1.and(qComplaintReportData.questionId.eq(192L));
            if (StringUtils.isNotEmpty(complaintSearch.getStationCode())){
                flag = true;
                builder1.and(qComplaintReportData.answerOther.in(complaintSearch.getStationCode()));
            }
            if (null != complaintSearch.getComplaintNum() && complaintSearch.getComplaintNum() > 0) {
                flag = true;
                List<VStarCodeNum> list = (List<VStarCodeNum>) vStarCodeNumRepository.findByNum(complaintSearch.getComplaintNum());
                if (list == null || list.size() == 0) {
                    return new ArrayList<>();
                }
                BooleanBuilder builderOr = new BooleanBuilder();
                for (VStarCodeNum vStarCodeNum : list) {
                    builderOr.or(qComplaintReportData.answerOther.eq(vStarCodeNum.getAnswer()));
                }
                builder1.and(builderOr);
            }
            if (flag) {
                List<ComplaintReportData> reportData = (List<ComplaintReportData>) complaintReportDataRepository.findAll(builder1);
                if (reportData == null || reportData.size() == 0) {
                    return new ArrayList<>();
                }
                List<Long> Ids = new ArrayList<>();
                for (ComplaintReportData data : reportData) {
                    Ids.add(data.getComplaintReport().getId());
                }
                builder.and(qComplaintReport.id.in(Ids));
            }
        }
        //漆星服务代码
        if (StringUtils.isNotEmpty(complaintSearch.getvStarCode())) {
            BooleanBuilder builder1 = new BooleanBuilder();
            builder1.and(qComplaintReportData.enabled.eq(true));
            builder1.and(qComplaintReportData.deleted.eq(false));
            builder1.and(qComplaintReportData.questionId.eq(191L));
            builder1.and(qComplaintReportData.answerOther.eq(complaintSearch.getvStarCode()));
            List<ComplaintReportData> reportData = (List<ComplaintReportData>) complaintReportDataRepository.findAll(builder1);
            if (reportData == null || reportData.size() == 0) {
                return new ArrayList<>();
            }
            List<Long> Ids = new ArrayList<>();
            for (ComplaintReportData data : reportData) {
                Ids.add(data.getComplaintReport().getId());
            }
            builder.and(qComplaintReport.id.in(Ids));

        }
        if (StringUtils.isNotEmpty(complaintSearch.getStationName()) || StringUtils.isNotEmpty(complaintSearch.getComplaintType())){
            BooleanBuilder builder1 = new BooleanBuilder();
            builder1.and(qComplaintReportData.deleted.eq(false));
            builder1.and(qComplaintReportData.enabled.eq(true));
            boolean flag = false;
            if (StringUtils.isNotEmpty(complaintSearch.getStationName())) {
                flag = true;
                builder1.and(qComplaintReportData.questionId.eq(193L));
                builder1.and(qComplaintReportData.answerOther.like("%" + complaintSearch.getStationName() + "%"));
            }
            if (StringUtils.isNotEmpty(complaintSearch.getComplaintType())){
                flag = true;
                builder1.and(qComplaintReportData.questionId.eq(195L));
                String[] types = complaintSearch.getComplaintType().split(",");
                BooleanBuilder builderOr = new BooleanBuilder();
                for (String str: types){
                    builderOr.or(qComplaintReportData.answerId.eq("%"+str+"%"));
                }
                builder1.and(builderOr);
            }
            if (flag) {
                List<ComplaintReportData> list = (List<ComplaintReportData>) complaintReportDataRepository.findAll(builder1);
                if (list == null || list.size() == 0) {
                    return new ArrayList<>();
                }
                List<Long> ids = new ArrayList<>();
                for (ComplaintReportData reportData : list) {
                    ids.add(reportData.getComplaintReport().getId());
                }
                builder.and(qComplaintReport.id.in(ids));
            }
        }
        List<ComplaintReport> complaintReportList = (List<ComplaintReport>) complaintReportRepository.findAll(builder);
        List<Long> reportIds = new ArrayList<>();
        for (ComplaintReport report : complaintReportList) {
            reportIds.add(report.getId());
        }
        return reportIds;
    }

    /**
     * 通过条件获取符合的报告ID
     *
     * @param complaintSearch
     * @return
     */
    @Override
    public List<Long> searchComplaintReportId(ComplaintSearch complaintSearch, User clientUser) {
        List<Long> complaintIds = new ArrayList<>();
        //判断用户负责的省份
        UserProfile userProfile = null;
        if (null != clientUser) {
            userProfile = userProfileRepository.findByUserId(clientUser.getId());
        }
        ProvinceBrandPoJo provinceBrandPoJo = provinceBrandSearchHelper.
                getUserSearchProvinceBrand(userProfile, complaintSearch.getProvince(),
                        complaintSearch.getBrandId(), false);
        List<String> searchProvinces = new ArrayList<>();
        List<String> searchBrand = new ArrayList<>();
        if (null == clientUser) {
            if (StringUtils.isNotEmpty(complaintSearch.getProvince()) && StringUtils.isNotEmpty(complaintSearch.getBrandId())) {
                for (String province : complaintSearch.getProvince().split(",")) {
                    searchProvinces.add(province);
                }
                for (String id : complaintSearch.getBrandId().split(",")) {
                    searchBrand.add(id);
                }
                complaintIds = getDataByProvince_Brand(searchProvinces, searchBrand, complaintIds);
            } else {
                List<Nation> nationList = userProfile.getNationList();
                List<String> nationStr = new ArrayList<>();
                for (Nation nation : nationList) {
                    nationStr.add(nation.getProvince());
                }
                if (StringUtils.isNotEmpty(complaintSearch.getProvince())) {
                    for (String province : complaintSearch.getProvince().split(",")) {
                        if (nationStr.contains(province)) {
                            searchProvinces.add(province);
                        }
                    }
                    complaintIds = getDataByArrayQuestion(searchProvinces, 189L, complaintIds);
                }
                List<Brand> brandList = userProfile.getBrandList();
                List<String> brandStr = new ArrayList<>();
                for (Brand b : brandList) {
                    brandStr.add(b.getBrandName());
                }
                if (StringUtils.isNotEmpty(complaintSearch.getBrandId())) {
                    for (String id : complaintSearch.getBrandId().split(",")) {
                        QuestionAnswerOption questionAnswerOption = questionAnswerOptionRepository.findOne(Long.valueOf(id));
                        if (null != questionAnswerOption&&questionAnswerOption.getName()!=null) {
                            if (brandStr.contains(questionAnswerOption.getName())) {
                                searchBrand.add(id);
                            }
                        }
                    }
                    complaintIds = getDataByArrayQuestion(searchBrand, 194L, complaintIds);
                }
            }
        } else {
            //筛选经销商报告
            if (clientUser.getUserType().equals(UserType.dealers)) {
                List<Station> stationList = userProfile.getDepartmentList().get(0).getStationList();
                List<String> stationNumList = new ArrayList<>();
                for (Station station : stationList) {
                    if (StringUtils.isNotEmpty(station.getStationNum())) {
                        stationNumList.add(station.getStationNum());
                    }
                }
                //如果经销商通过维修站编号筛选出报告直接返回空
                if (stationNumList.isEmpty()) {
                    return new ArrayList<>();
                }
                complaintIds = getDataByStationNum(stationNumList, complaintSearch.getProvince(), complaintSearch.getBrandId());
                //如果省份与品牌没有筛选出报告直接返回空
                if (complaintIds.isEmpty()) {
                    return new ArrayList<>();
                }
            } else {
                if (StringUtils.isEmpty(complaintSearch.getProvince()) &&
                        StringUtils.isEmpty(complaintSearch.getBrandId())) {
                    if (provinceBrandPoJo.getOwnProvinceList().isEmpty()) {
                        complaintIds = getComplaintNumByBrand(provinceBrandPoJo.getOwnBrandList(), null);
                    } else if (provinceBrandPoJo.getOwnBrandList().isEmpty()) {
                        complaintIds = getComplaintNumByProvince(provinceBrandPoJo.getOwnProvinceList(), null, null);
                    } else {
                        List<Long> provinceComplaintId =
                                getComplaintNumByProvince(provinceBrandPoJo.getOwnProvinceList(), null, null);
                        complaintIds = getComplaintNumByBrand(provinceBrandPoJo.getOwnBrandList(), null);
                        complaintIds.addAll(provinceComplaintId);
                    }
                }
                if (StringUtils.isNotEmpty(complaintSearch.getProvince()) &&
                        StringUtils.isNotEmpty(complaintSearch.getBrandId())) {
                    //无匹省份权限+无品牌权限
                    if (provinceBrandPoJo.getOwnProvinceList().isEmpty() && provinceBrandPoJo.getOwnBrandList().isEmpty()) {
                        return new ArrayList<>();
                    }
                    //经销商-->销售 身份权限为空 品牌权限不为空
                    if (provinceBrandPoJo.getOwnProvinceList().isEmpty() && !provinceBrandPoJo.getOwnBrandList().isEmpty()) {
                        if (provinceBrandPoJo.getCanBrandList().isEmpty()) {
                            return new ArrayList<>();
                        }
                        List<Long> brandReportId = getComplaintNumByBrand(provinceBrandPoJo.getCanBrandList(), null);
                        complaintIds = getComplaintNumByProvince(null, provinceBrandPoJo.getSearchProvinceList(), brandReportId);
                    }
                    //经销商-->销售 身份权限不为空 品牌权限为空
                    if (!provinceBrandPoJo.getOwnProvinceList().isEmpty() && provinceBrandPoJo.getOwnBrandList().isEmpty()) {
                        if (provinceBrandPoJo.getCanProvinceList().isEmpty()) {
                            return new ArrayList<>();
                        }
                        List<Long> provinceComplaintId =
                                getComplaintNumByProvince(provinceBrandPoJo.getCanProvinceList(), null, null);
                        complaintIds = getComplaintNumByBrand(provinceBrandPoJo.getSearchBrandList(), provinceComplaintId);
                    }
                    //如果省份和品牌都没有匹配就返回空
                    if (provinceBrandPoJo.getCanProvinceList().isEmpty() &&
                            provinceBrandPoJo.getCanBrandList().isEmpty()) {
                        return new ArrayList<>();
                    }
                    //如果有匹配条件的筛选省份+无匹配条件的筛选品牌
                    if (!provinceBrandPoJo.getCanProvinceList().isEmpty() &&
                            provinceBrandPoJo.getCanBrandList().isEmpty()) {
                        List<Long> reportId = getComplaintNumByBrand(provinceBrandPoJo.getSearchBrandList(), null);
                        if (reportId.isEmpty()) {
                            return new ArrayList<>();
                        }
                        complaintIds = getComplaintNumByProvince(provinceBrandPoJo.getCanProvinceList(), null, reportId);
                    }
                    //如果无匹配条件的筛选省份+有匹配条件的筛选品牌
                    if (provinceBrandPoJo.getCanProvinceList().isEmpty() &&
                            !provinceBrandPoJo.getCanBrandList().isEmpty()) {
                        if (provinceBrandPoJo.getOwnProvinceList().isEmpty()) {
                            List<Long> brandReportId = getComplaintNumByBrand(provinceBrandPoJo.getCanBrandList(), null);
                            complaintIds = getComplaintNumByProvince(null, provinceBrandPoJo.getSearchProvinceList(), brandReportId);
                        } else {
                            List<Long> reportId = getComplaintNumByProvince(null, provinceBrandPoJo.getSearchProvinceList(), null);
                            if (reportId.isEmpty()) {
                                return new ArrayList<>();
                            }

                            complaintIds = getComplaintNumByBrand(provinceBrandPoJo.getCanBrandList(), reportId);
                        }
                    }
                    //如果筛选省份+筛选品牌都匹配
                    if (!provinceBrandPoJo.getCanProvinceList().isEmpty() &&
                            !provinceBrandPoJo.getCanBrandList().isEmpty()) {
                        List<Long> reportId = getComplaintNumByProvince(provinceBrandPoJo.getCanProvinceList(), null, null);
                        complaintIds = getComplaintNumByBrand(provinceBrandPoJo.getCanBrandList(), reportId);
                    }
                } else {
                    //筛选条件只有省份时
                    if (StringUtils.isNotEmpty(complaintSearch.getProvince())) {
                        //当省份权限或者品牌权限一个为空时
                        if (provinceBrandPoJo.getOwnProvinceList().isEmpty() || provinceBrandPoJo.getOwnBrandList().isEmpty()) {
                            //省份权限为空 品牌权限不为空
                            if (provinceBrandPoJo.getOwnProvinceList().isEmpty() && !provinceBrandPoJo.getOwnBrandList().isEmpty()) {
                                List<Long> provinceComplaintId = getComplaintNumByProvince(null, provinceBrandPoJo.getSearchProvinceList(), null);
                                complaintIds = getComplaintNumByBrand(provinceBrandPoJo.getOwnBrandList(), provinceComplaintId);
                            }
                            //省份权限不为空 品牌权限为空
                            if (!provinceBrandPoJo.getOwnProvinceList().isEmpty() && provinceBrandPoJo.getOwnBrandList().isEmpty()) {
                                if (provinceBrandPoJo.getCanProvinceList().isEmpty()) {
                                    return new ArrayList<>();
                                } else {
                                    complaintIds = getComplaintNumByProvince(provinceBrandPoJo.getCanProvinceList(), null, null);
                                }
                            }
                        } else {
                            //省份权限 品牌权限都不为空时
                            List<Long> brandComplaintId = getComplaintNumByBrand(provinceBrandPoJo.getOwnBrandList(), null);
                            complaintIds = getComplaintNumByProvince(null, provinceBrandPoJo.getSearchProvinceList(), brandComplaintId);
                        }
                    }
                    //筛选条件只有品牌时
                    if (StringUtils.isNotEmpty(complaintSearch.getBrandId())) {
                        //当省份权限或者品牌权限一个为空时
                        if (provinceBrandPoJo.getOwnProvinceList().isEmpty() || provinceBrandPoJo.getOwnBrandList().isEmpty()) {
                            //省份权限为空 品牌权限不为空
                            if (provinceBrandPoJo.getOwnProvinceList().isEmpty() && !provinceBrandPoJo.getOwnBrandList().isEmpty()) {
                                if (provinceBrandPoJo.getCanBrandList().isEmpty()) {
                                    return new ArrayList<>();
                                } else {
                                    complaintIds = getComplaintNumByBrand(provinceBrandPoJo.getCanBrandList(), null);
                                }
                            }
                            //省份权限不为空 品牌权限为空
                            if (!provinceBrandPoJo.getOwnProvinceList().isEmpty() && provinceBrandPoJo.getOwnBrandList().isEmpty()) {
                                List<Long> provinceComplaintId = getComplaintNumByProvince(provinceBrandPoJo.getOwnProvinceList(), null, null);
                                complaintIds = getComplaintNumByBrand(provinceBrandPoJo.getSearchBrandList(), provinceComplaintId);
                            }
                        } else {
                            //省份权限 品牌权限都不为空时
                            List<Long> provinceComplaintId = getComplaintNumByProvince(provinceBrandPoJo.getOwnProvinceList(), null, null);
                            complaintIds = getComplaintNumByBrand(provinceBrandPoJo.getSearchBrandList(), provinceComplaintId);
                        }
                    }
                }
            }
        }
        if (null != complaintSearch.getValidTime()) {
            List<Long> validId = getValidTimeReport(complaintIds, complaintSearch.getValidTime());
            if (validId.isEmpty()) {
                return new ArrayList<>();
            } else {
                complaintIds = validId;
            }
        }
        if (null != complaintSearch.getComplaintType() && complaintSearch.getComplaintType() != null) {
            String[] types = complaintSearch.getComplaintType().split(",");
            List<Long> typeId = getDataByQuestion(null, 195L, types, complaintIds);
            if (typeId.isEmpty()) {
                return new ArrayList<>();
            } else {
                complaintIds = typeId;
            }
        }
        if (StringUtils.isNotEmpty(complaintSearch.getStationName())) {
            List<Long> nameId = getDataByQuestion(complaintSearch.getStationName(), 193L, null, complaintIds);
            if (nameId.isEmpty()) {
                return new ArrayList<>();
            } else {
                complaintIds = nameId;
            }
        }
        if (StringUtils.isNotEmpty(complaintSearch.getStationCode())) {
            List<Long> stationCodeId = getDataByQuestion(complaintSearch.getStationCode(), 191L, null, complaintIds);
            if (stationCodeId.isEmpty()) {
                return new ArrayList<>();
            } else {
                complaintIds = stationCodeId;
            }
        }
        if (StringUtils.isNotEmpty(complaintSearch.getvStarCode())) {
            List<Long> vStarCodeId = getDataByQuestion(complaintSearch.getvStarCode(), 192L, null, complaintIds);
            if (vStarCodeId.isEmpty()) {
                return new ArrayList<>();
            } else {
                complaintIds = vStarCodeId;
            }
        }
        if (null != complaintSearch.getComplaintNum() && complaintSearch.getComplaintNum() > 0) {
            List<Long> complaintNumId = getComplaintNumStation(complaintSearch.getComplaintNum(), complaintIds);
            if (complaintNumId.isEmpty()) {
                return new ArrayList<>();
            } else {
                complaintIds = complaintNumId;
            }
        }
        return complaintIds;
    }

    //通过省份查询投诉报告
    private List<Long> getComplaintNumByProvince(List<String> provinceStrList, String[] searchProvince, List<Long> complaintIds) {
        QComplaintReportData qComplaintReportData = QComplaintReportData.complaintReportData;
        BooleanBuilder builder = getComplaintReportBuilder(189L, complaintIds);
        if (null != searchProvince && searchProvince.length != 0) {
            builder.and(qComplaintReportData.answerOther.in(searchProvince));
        }
        if (null != provinceStrList && !provinceStrList.isEmpty()) {
            builder.and(qComplaintReportData.answerOther.in(provinceStrList));
        }
        List<Long> complaintReportId = new ArrayList<>();
        List<ComplaintReportData> complaintReportDataList =
                (List<ComplaintReportData>) complaintReportDataRepository.findAll(builder);
        for (ComplaintReportData complaintReportData : complaintReportDataList) {
            complaintReportId.add(complaintReportData.getComplaintReport().getId());
        }
        return complaintReportId;
    }

    //通过品牌查询投诉报告
    private List<Long> getComplaintNumByBrand(List<Long> complaintBandId, List<Long> complaintIds) {
        Map<Long, Integer> complaintReportId = new HashMap<>();
        QComplaintReportData qComplaintReportData = QComplaintReportData.complaintReportData;
        for (Long brandId : complaintBandId) {
            BooleanBuilder builder = getComplaintReportBuilder(194L, complaintIds);
            builder.and(qComplaintReportData.answerId.like("%" + brandId + "%"));
            List<ComplaintReportData> complaintReportDataList =
                    (List<ComplaintReportData>) complaintReportDataRepository.findAll(builder);
            for (ComplaintReportData complaintReportData : complaintReportDataList) {
                complaintReportId.put(complaintReportData.getComplaintReport().getId(), 1);
            }
        }
        return new ArrayList<>(complaintReportId.keySet());
    }

    private BooleanBuilder getComplaintReportBuilder(Long questionId, List<Long> complaintIds) {
        QComplaintReportData qComplaintReportData = QComplaintReportData.complaintReportData;
        BooleanBuilder builder = new BooleanBuilder();
        builder.and(qComplaintReportData.complaintReport.deleted.eq(false));
        builder.and(qComplaintReportData.complaintReport.enabled.eq(true));
        builder.and(qComplaintReportData.complaintReport.auditFalse.eq(false));
        if (null != complaintIds && !complaintIds.isEmpty()) {
            builder.and(qComplaintReportData.complaintReport.id.in(complaintIds));
        }
        builder.and(qComplaintReportData.questionId.eq(questionId));
        return builder;
    }

    /**
     * 查询投诉次数
     *
     * @param complaintNum
     * @return
     */
    private List<Long> getComplaintNumStation(Integer complaintNum, List<Long> complaintIds) {
        BooleanBuilder builder = new BooleanBuilder();
        QComplaintReport qComplaintReport = QComplaintReport.complaintReport;
        builder.and(qComplaintReport.deleted.eq(false));
        builder.and(qComplaintReport.enabled.eq(true));
        builder.and(qComplaintReport.auditFalse.eq(false));
        if (!complaintIds.isEmpty()) {
            builder.and(qComplaintReport.id.in(complaintIds));
        }
        List<ComplaintReport> complaintReportList = (List<ComplaintReport>) complaintReportRepository.findAll(builder);
        Map<String, ComplaintNum> num = new HashMap<>();
        for (ComplaintReport report : complaintReportList) {
            ComplaintReportData data = complaintReportDataRepository.selectByQuestion(report.getId(), 192L);
            if (null == data || StringUtils.isEmpty(data.getAnswerOther())) {
                continue;
            }
            if (num.containsKey(data.getAnswerOther())) {
                ComplaintNum reportNum = num.get(data.getAnswerOther());
                List<Long> reportIds = reportNum.getComplaintReportId();
                reportIds.add(data.getComplaintReport().getId());
                reportNum.setComplaintReportId(reportIds);

                Integer value = reportNum.getNum();
                reportNum.setNum(value + 1);
                num.put(data.getAnswerOther(), reportNum);
            } else {
                ComplaintNum reportNum = new ComplaintNum();
                List<Long> reportIds = new ArrayList<>();
                reportIds.add(data.getComplaintReport().getId());
                reportNum.setComplaintReportId(reportIds);
                num.put(data.getAnswerOther(), reportNum);
            }
        }
        List<Long> reportIds = new ArrayList<>();
        for (Map.Entry<String, ComplaintNum> map : num.entrySet()) {
            ComplaintNum reportNum = map.getValue();
            if (reportNum.getNum().equals(complaintNum)) {
                reportIds.addAll(reportNum.getComplaintReportId());
            }
        }
        if (reportIds.isEmpty()) {
            complaintIds = new ArrayList<>();
        } else {
            complaintIds = reportIds;
        }
        return complaintIds;
    }

    /**
     * 通过关键字以及答案查询投诉单
     *
     * @param strValue
     * @param questionId
     * @param answerIds
     * @param complaintIds
     * @return
     */
    private List<Long> getDataByQuestion(String strValue, Long questionId, String[] answerIds, List<Long> complaintIds) {
        BooleanBuilder builder = new BooleanBuilder();
        QComplaintReportData qComplaintReportData = QComplaintReportData.complaintReportData;
        builder.and(qComplaintReportData.deleted.eq(false));
        builder.and(qComplaintReportData.enabled.eq(true));
        builder.and(qComplaintReportData.questionId.eq(questionId));
        if (!complaintIds.isEmpty()) {
            builder.and(qComplaintReportData.complaintReport.id.in(complaintIds));
        }
        if (StringUtils.isNotEmpty(strValue)) {
            builder.and(qComplaintReportData.answerOther.like("%" + strValue + "%"));
        }
        if (questionId.equals(188L)) {
            builder.and(qComplaintReportData.answerId.in(answerIds));
        }
        if (questionId.equals(194L) || questionId.equals(195L)) {
            BooleanBuilder builderOr = new BooleanBuilder();
            for (String str:answerIds) {
                builderOr.or(qComplaintReportData.answerId.like("%" + str + "%"));
            }
            builder.and(builderOr);
        }
        List<ComplaintReportData> dataList = (List<ComplaintReportData>) complaintReportDataRepository.findAll(builder);
        List<Long> reportIds = new ArrayList<>();
        for (ComplaintReportData data : dataList) {
            reportIds.add(data.getComplaintReport().getId());
        }
        return reportIds;
    }
    private List<Long> getDataByStationNum(String stationNumList, String province, String brandIds) {
        Map<Long, Integer> dataIdList = new HashMap<>();
        if (!stationNumList.isEmpty()) {
            BooleanBuilder ooBuilder = new BooleanBuilder();
            QComplaintReportData orComplaintReportData = QComplaintReportData.complaintReportData;
            ooBuilder.and(orComplaintReportData.deleted.eq(false));
            ooBuilder.and(orComplaintReportData.enabled.eq(true));
            ooBuilder.and(orComplaintReportData.questionId.eq(192L));
            ooBuilder.and(orComplaintReportData.answerOther.eq(stationNumList));
            List<ComplaintReportData> orDataList = (List<ComplaintReportData>) complaintReportDataRepository.findAll(ooBuilder);
            for (ComplaintReportData complaintReportData : orDataList) {
                dataIdList.put(complaintReportData.getComplaintReport().getId(), 1);
            }
        } else {
            return new ArrayList<>();
        }
        Map<Long, Integer> provinceComplaintIds = new HashMap<>();
        if (StringUtils.isNotEmpty(province) && !dataIdList.isEmpty()) {
            String[] provinceArray = province.split(",");
            BooleanBuilder ooBuilder = new BooleanBuilder();
            QComplaintReportData orComplaintReportData = QComplaintReportData.complaintReportData;
            ooBuilder.and(orComplaintReportData.deleted.eq(false));
            ooBuilder.and(orComplaintReportData.enabled.eq(true));
            ooBuilder.and(orComplaintReportData.questionId.eq(189L));
            ooBuilder.and(orComplaintReportData.answerOther.in(provinceArray));
            if (!dataIdList.isEmpty()) {
                ooBuilder.and(orComplaintReportData.complaintReport.id.in(dataIdList.keySet()));
            }
            List<ComplaintReportData> orDataList = (List<ComplaintReportData>) complaintReportDataRepository.findAll(ooBuilder);
            if (orDataList.isEmpty()) {
                return new ArrayList<>();
            }
            for (ComplaintReportData complaintReportData : orDataList) {
                provinceComplaintIds.put(complaintReportData.getComplaintReport().getId(), 1);
            }
        }

        Map<Long, Integer> brandComplaintId = new HashMap<>();
        if (StringUtils.isNotEmpty(brandIds) && !dataIdList.isEmpty()) {
            BooleanBuilder ooBuilder = new BooleanBuilder();
            QComplaintReportData orComplaintReportData = QComplaintReportData.complaintReportData;
            ooBuilder.and(orComplaintReportData.deleted.eq(false));
            ooBuilder.and(orComplaintReportData.enabled.eq(true));
            ooBuilder.and(orComplaintReportData.questionId.eq(194L));
            if (!provinceComplaintIds.isEmpty()) {
                ooBuilder.and(orComplaintReportData.complaintReport.id.in(provinceComplaintIds.keySet()));
                provinceComplaintIds = new HashMap<>();
            } else {
                if (!dataIdList.isEmpty()) {
                    ooBuilder.and(orComplaintReportData.complaintReport.id.in(dataIdList.keySet()));
                }
            }
            String[] brandArray = brandIds.split(",");
            for (String brandId : brandArray) {
                ooBuilder.and(orComplaintReportData.answerId.like("%" + brandId + "%"));
                List<ComplaintReportData> orDataList = (List<ComplaintReportData>) complaintReportDataRepository.findAll(ooBuilder);
                for (ComplaintReportData complaintReportData : orDataList) {
                    brandComplaintId.put(complaintReportData.getComplaintReport().getId(), 1);
                }
            }
            if (brandComplaintId.isEmpty()) {
                return new ArrayList<>();
            }
        }
        if (!brandComplaintId.isEmpty()) {
            return new ArrayList<>(brandComplaintId.keySet());
        } else if (!provinceComplaintIds.isEmpty()) {
            return new ArrayList<>(provinceComplaintIds.keySet());
        } else {
            return new ArrayList<>(dataIdList.keySet());
        }
    }
    private List<Long> getDataByStationNum(List<String> stationNumList, String province, String brandIds) {
        Map<Long, Integer> dataIdList = new HashMap<>();
        if (!stationNumList.isEmpty()) {
            BooleanBuilder ooBuilder = new BooleanBuilder();
            QComplaintReportData orComplaintReportData = QComplaintReportData.complaintReportData;
            ooBuilder.and(orComplaintReportData.deleted.eq(false));
            ooBuilder.and(orComplaintReportData.enabled.eq(true));
            ooBuilder.and(orComplaintReportData.questionId.eq(192L));
            ooBuilder.and(orComplaintReportData.answerOther.in(stationNumList));
            List<ComplaintReportData> orDataList = (List<ComplaintReportData>) complaintReportDataRepository.findAll(ooBuilder);
            for (ComplaintReportData complaintReportData : orDataList) {
                dataIdList.put(complaintReportData.getComplaintReport().getId(), 1);
            }
        } else {
            return new ArrayList<>();
        }
        Map<Long, Integer> provinceComplaintIds = new HashMap<>();
        if (StringUtils.isNotEmpty(province) && !dataIdList.isEmpty()) {
            String[] provinceArray = province.split(",");
            BooleanBuilder ooBuilder = new BooleanBuilder();
            QComplaintReportData orComplaintReportData = QComplaintReportData.complaintReportData;
            ooBuilder.and(orComplaintReportData.deleted.eq(false));
            ooBuilder.and(orComplaintReportData.enabled.eq(true));
            ooBuilder.and(orComplaintReportData.questionId.eq(189L));
            ooBuilder.and(orComplaintReportData.answerOther.in(provinceArray));
            if (!dataIdList.isEmpty()) {
                ooBuilder.and(orComplaintReportData.complaintReport.id.in(dataIdList.keySet()));
            }
            List<ComplaintReportData> orDataList = (List<ComplaintReportData>) complaintReportDataRepository.findAll(ooBuilder);
            if (orDataList.isEmpty()) {
                return new ArrayList<>();
            }
            for (ComplaintReportData complaintReportData : orDataList) {
                provinceComplaintIds.put(complaintReportData.getComplaintReport().getId(), 1);
            }
        }

        Map<Long, Integer> brandComplaintId = new HashMap<>();
        if (StringUtils.isNotEmpty(brandIds) && !dataIdList.isEmpty()) {
            BooleanBuilder ooBuilder = new BooleanBuilder();
            QComplaintReportData orComplaintReportData = QComplaintReportData.complaintReportData;
            ooBuilder.and(orComplaintReportData.deleted.eq(false));
            ooBuilder.and(orComplaintReportData.enabled.eq(true));
            ooBuilder.and(orComplaintReportData.questionId.eq(194L));
            if (!provinceComplaintIds.isEmpty()) {
                ooBuilder.and(orComplaintReportData.complaintReport.id.in(provinceComplaintIds.keySet()));
                provinceComplaintIds = new HashMap<>();
            } else {
                if (!dataIdList.isEmpty()) {
                    ooBuilder.and(orComplaintReportData.complaintReport.id.in(dataIdList.keySet()));
                }
            }
            String[] brandArray = brandIds.split(",");
            for (String brandId : brandArray) {
                ooBuilder.and(orComplaintReportData.answerId.like("%" + brandId + "%"));
                List<ComplaintReportData> orDataList = (List<ComplaintReportData>) complaintReportDataRepository.findAll(ooBuilder);
                for (ComplaintReportData complaintReportData : orDataList) {
                    brandComplaintId.put(complaintReportData.getComplaintReport().getId(), 1);
                }
            }
            if (brandComplaintId.isEmpty()) {
                return new ArrayList<>();
            }
        }
        if (!brandComplaintId.isEmpty()) {
            return new ArrayList<>(brandComplaintId.keySet());
        } else if (!provinceComplaintIds.isEmpty()) {
            return new ArrayList<>(provinceComplaintIds.keySet());
        } else {
            return new ArrayList<>(dataIdList.keySet());
        }
    }

    /**
     * 当省份和品牌都有值的时候
     *
     * @param provinceArray
     * @param brandArray
     * @param complaintIds
     * @return
     */
    private List<Long> getDataByProvince_Brand(List<String> provinceArray, List<String> brandArray, List<Long> complaintIds) {
        Map<Long, Integer> dataIdList = new HashMap<>();
        for (String province : provinceArray) {
            BooleanBuilder ooBuilder = new BooleanBuilder();
            QComplaintReportData orComplaintReportData = QComplaintReportData.complaintReportData;
            ooBuilder.and(orComplaintReportData.deleted.eq(false));
            ooBuilder.and(orComplaintReportData.enabled.eq(true));
            ooBuilder.and(orComplaintReportData.questionId.eq(189L));
            ooBuilder.and(orComplaintReportData.answerOther.like("%" + province + "%"));
            List<ComplaintReportData> ordataList = (List<ComplaintReportData>) complaintReportDataRepository.findAll(ooBuilder);
            for (ComplaintReportData complaintReportData : ordataList) {
                dataIdList.put(complaintReportData.getId(), 1);
            }
        }
        Map<Long, Integer> IdList = new HashMap<>();
        for (String brand : brandArray) {
            BooleanBuilder ooBuilder = new BooleanBuilder();
            QComplaintReportData orComplaintReportData = QComplaintReportData.complaintReportData;
            ooBuilder.and(orComplaintReportData.deleted.eq(false));
            ooBuilder.and(orComplaintReportData.enabled.eq(true));
            ooBuilder.and(orComplaintReportData.complaintReport.id.in(new ArrayList<>(dataIdList.keySet())));
            ooBuilder.and(orComplaintReportData.questionId.eq(194L));
            ooBuilder.and(orComplaintReportData.answerId.like("%" + brand + "%"));
            List<ComplaintReportData> ordataList = (List<ComplaintReportData>) complaintReportDataRepository.findAll(ooBuilder);
            for (ComplaintReportData complaintReportData : ordataList) {
                IdList.put(complaintReportData.getId(), 1);
            }
        }
        List<Long> reportIds = new ArrayList<>(IdList.keySet());
        //去除重复
        complaintIds.removeAll(reportIds);
        //合并
        complaintIds.addAll(reportIds);
        return complaintIds;
    }

    /**
     * 通过关键字以及答案查询投诉单
     *
     * @param strArrayValue
     * @param questionId
     * @param complaintIds
     * @return
     */
    private List<Long> getDataByArrayQuestion(List<String> strArrayValue, Long questionId, List<Long> complaintIds) {
        Map<Long, Integer> dataIdList = new HashMap<>();
        for (String province : strArrayValue) {
            BooleanBuilder ooBuilder = new BooleanBuilder();
            QComplaintReportData orComplaintReportData = QComplaintReportData.complaintReportData;
            ooBuilder.and(orComplaintReportData.deleted.eq(false));
            ooBuilder.and(orComplaintReportData.enabled.eq(true));
            ooBuilder.and(orComplaintReportData.questionId.eq(questionId));
            if (questionId.equals(189L)) {
                ooBuilder.and(orComplaintReportData.answerOther.like("%" + province + "%"));
            }
            if (questionId.equals(194L)) {
                if (province.equals("447")) {
                    ooBuilder.and(orComplaintReportData.answerOther.isNotEmpty());
                } else {
                    ooBuilder.and(orComplaintReportData.answerId.like("%" + province + "%"));
                }
            }
            List<ComplaintReportData> ordataList = (List<ComplaintReportData>) complaintReportDataRepository.findAll(ooBuilder);
            for (ComplaintReportData complaintReportData : ordataList) {
                dataIdList.put(complaintReportData.getComplaintReport().getId(), 1);
            }
        }

        if (!dataIdList.isEmpty()) {
            List<Long> reportIds = new ArrayList<>();
            BooleanBuilder builder = new BooleanBuilder();
            QComplaintReportData qComplaintReportData = QComplaintReportData.complaintReportData;
            builder.and(qComplaintReportData.deleted.eq(false));
            builder.and(qComplaintReportData.enabled.eq(true));
            builder.and(qComplaintReportData.questionId.eq(questionId));
            builder.and(qComplaintReportData.id.in(new ArrayList<>(dataIdList.keySet())));
            List<ComplaintReportData> dataList = (List<ComplaintReportData>) complaintReportDataRepository.findAll(builder);
            for (ComplaintReportData data : dataList) {
                reportIds.add(data.getComplaintReport().getId());
            }
            //去除重复
            complaintIds.removeAll(reportIds);
            //合并
            complaintIds.addAll(reportIds);
        }
        return complaintIds;
    }

    /**
     * 如果品牌的条件是其他品牌 查询出questionId为194 并且answerOther不为空的记录
     *
     * @param questionId
     * @param complaintIds
     * @return
     */
    private List<Long> getOtherBrandComplaint(Long questionId, List<Long> complaintIds) {
        BooleanBuilder builder = new BooleanBuilder();
        QComplaintReportData qComplaintReportData = QComplaintReportData.complaintReportData;
        builder.and(qComplaintReportData.deleted.eq(false));
        builder.and(qComplaintReportData.enabled.eq(true));
        builder.and(qComplaintReportData.questionId.eq(questionId));
        builder.or(qComplaintReportData.answerOther.isNotEmpty());
        if (!complaintIds.isEmpty()) {
            builder.and(qComplaintReportData.complaintReport.id.in(complaintIds));
        }
        List<ComplaintReportData> dataList = (List<ComplaintReportData>) complaintReportDataRepository.findAll(builder);
        List<Long> reportIds = new ArrayList<>();
        for (ComplaintReportData data : dataList) {
            reportIds.add(data.getComplaintReport().getId());
        }
        if (reportIds.isEmpty()) {
            complaintIds = new ArrayList<>();
        }
        //去除重复
        complaintIds.removeAll(reportIds);
        //合并
        complaintIds.addAll(reportIds);
        return complaintIds;
    }

    /**
     * 查询其他品牌
     *
     * @param brand
     * @param complaintIds
     * @return
     */
    private List<Long> getOtherBrandComplaint(String brand, List<Long> complaintIds) {
        BooleanBuilder builder = new BooleanBuilder();
        QComplaintReportData qComplaintReportData = QComplaintReportData.complaintReportData;
        builder.and(qComplaintReportData.deleted.eq(false));
        builder.and(qComplaintReportData.enabled.eq(true));
        builder.and(qComplaintReportData.answerOther.like("%" + brand + "%"));
        if (!complaintIds.isEmpty()) {
            builder.and(qComplaintReportData.complaintReport.id.in(complaintIds));
        }
        List<ComplaintReportData> dataList = (List<ComplaintReportData>) complaintReportDataRepository.findAll(builder);
        List<Long> reportIds = new ArrayList<>();
        for (ComplaintReportData data : dataList) {
            reportIds.add(data.getComplaintReport().getId());
        }
        if (reportIds.isEmpty()) {
            complaintIds = new ArrayList<>();
        }
        //去除重复
        complaintIds.removeAll(reportIds);
        //合并
        complaintIds.addAll(reportIds);
        return complaintIds;
    }


    private List<Long> getValidTimeReport(List<Long> complaintIds, boolean isValidTime) {
        BooleanBuilder builder = new BooleanBuilder();
        QComplaintReport qComplaintReport = QComplaintReport.complaintReport;
        builder.and(qComplaintReport.deleted.eq(false));
        builder.and(qComplaintReport.enabled.eq(true));
        if (!complaintIds.isEmpty()) {
            builder.and(qComplaintReport.id.in(complaintIds));
        }
        List<ComplaintReport> reportList = (List<ComplaintReport>) complaintReportRepository.findAll(builder);
        List<Long> reportIds = new ArrayList<>();
        List<Long> unReportIds = new ArrayList<>();
        for (ComplaintReport report : reportList) {
            if (isValidTime) {
                if (null != report) {
                    if (!report.isTimeOut()) {
                        reportIds.add(report.getId());
                    }
                }
            } else {
                if (null != report) {
                    if (report.isTimeOut()) {
                        unReportIds.add(report.getId());
                    }
                }
            }
        }
        if (isValidTime) {
            return reportIds;
        } else {
            return unReportIds;
        }
    }

    /**
     * 是否在有效时间内完成
     *
     * @param complaintReport
     * @return
     */
    private boolean isTimeRemainingTime(ComplaintReport complaintReport) {
        double actualTime = 0.0;
        //获取现在时间 审核时间
        Long nowTime = System.currentTimeMillis();
        if (null == complaintReport.getAuditTime()) {
            return false;
        }
        Long auditTime = complaintReport.getAuditTime().getTime();
        //如果没有提交解决方案 实际时间=现在时间-审核时间
        if (null == complaintReport.getSolutionTime()) {
            actualTime = nowTime - auditTime;
        } else {//如果有提交解决方案  实际时间=解决方案时间-审核时间
            Long solutionTime = complaintReport.getSolutionTime().getTime();
            actualTime = solutionTime - auditTime;
        }
        actualTime = actualTime / 1000 / 60 / 60;

        //剩余时间=有效时间-实际时间
        //获取剩余时间
        double vaildTime = complaintReport.getValidTime();
        double timeRemaining = vaildTime - actualTime;
        if (timeRemaining < 0) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 验证用户的的省份权限
     *
     * @param userProfile
     * @param provinces
     * @return
     */
    private List<String> getUserProvinces(UserProfile userProfile, String provinces) {
        List<String> userProvinces = getUserProvinces(userProfile);
        List<String> searchProvinces = new ArrayList<>();
        for (String search : provinces.split(",")) {
            searchProvinces.add(search);
        }
        List<String> returnProvinces = new ArrayList<>();
        for (String search : searchProvinces) {
            for (String user : userProvinces) {
                if (search.equals(user)) {
                    returnProvinces.add(search);
                }
            }
        }
        return returnProvinces;
    }

    /**
     * 获取用户负责的省份
     *
     * @param userProfile
     * @return
     */
    private List<String> getUserProvinces(UserProfile userProfile) {
        List<Nation> nationList = userProfile.getNationList();
        Map<String, Integer> provincesMap = new HashMap<>();
        for (Nation nation : nationList) {
            provincesMap.put(nation.getProvince(), 1);
        }
        List<String> userProvinces = new ArrayList<>(provincesMap.keySet());
        return userProvinces;
    }

    /**
     * 验证用户负责品牌
     *
     * @param userProfile
     * @param brands
     * @return
     */
    private List<String> getUserBrand(UserProfile userProfile, String brands) {
        List<String> userBrandList = getUserBrand(userProfile);
        List<String> returnBrandId = new ArrayList<>();
        for (String searchBrand : brands.split(",")) {
            if (userBrandList.contains(searchBrand)) {
                returnBrandId.add(searchBrand);
            }
//            for (String brandId : userBrandList) {
//                try {
//                    if (searchBrand.equals(brandId)) {
//
//                    }
//                } catch (Exception e) {
//                    returnBrandId.add(searchBrand);
//                }
//            }
        }
        return returnBrandId;
    }

    /**
     * 获取用户的品牌
     *
     * @param userProfile
     * @return
     */
    private List<String> getUserBrand(UserProfile userProfile) {
        List<Brand> brandList = userProfile.getBrandList();
        Map<String, Integer> brandMap = new HashMap<>();
        for (Brand brand : brandList) {
            QuestionAnswerOption questionAnswerOption = questionAnswerOptionRepository.findComplaintBrandByName(brand.getBrandName());
            if (null != questionAnswerOption) {
                brandMap.put(questionAnswerOption.getId().toString(), 1);
            }
        }
        return new ArrayList<>(brandMap.keySet());
    }


}
